package org.gthelper.handler;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;

import org.gthelper.data.DataGeter;
import org.gthelper.database.UserInfo;
import org.gthelper.database.UserinfoStatus;
import org.gthelper.error.ErrorCode;
import org.gthelper.order.OrderInfo;


/**
 * 退出登录
 * @author root
 *
 */
public class Logout extends Handler
{

	@Override
	public int handle(OrderInfo orderinfo) {
		// TODO 自动生成的方法存根
		//get id
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataGeter dg = new DataGeter();
		int errcode=dg.getData(orderinfo.orderStream, baos);
		if(errcode!=ErrorCode.NORMAL)return errcode;
		byte[] userid = baos.toByteArray();
		baos.reset();
		String usrid="";
		try {
			usrid = new String(userid,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return ErrorCode.UNKNOWN;
		}
		
		try {
			UserInfo userinfo = UserInfo.getUserInfobyID(usrid);
			if(userinfo==null)return ErrorCode.IDNOTEXIST;
			userinfo.setstatus_login(UserinfoStatus.NotLogin);
			if(!userinfo.UpdateUserInfo())return ErrorCode.DATABASEERR;
			DataOutputStream dos = new DataOutputStream(orderinfo.mainOutput);
			dos.writeLong(1);
			dos.flush();
			dos.close();
			return ErrorCode.NORMAL;
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return ErrorCode.DATABASEERR;
		}
		
	}
	
}